//
//  CategoryTableViewController.swift
//  ToDoList
//
//  Created by Qingke on 16/4/14.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class CategoryTableViewController: UITableViewController {
    var categories = [CategoryClass]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let tc1 = CategoryClass(name: "读书")
        let tc2 = CategoryClass(name: "练车")
        let tc3 = CategoryClass(name: "卖衣服")
        categories += [tc1, tc2, tc3]
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    
   

    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return categories.count
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "cell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
        if  cell  == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
            cell?.accessoryType = .DetailDisclosureButton
            
            
        }
        let Item = categories[indexPath.row]
        cell?.textLabel?.text = Item.name

        return cell!
    }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //编辑内容页面
        let item = categories[indexPath.row]
        let navigation = storyboard!.instantiateViewControllerWithIdentifier("navigationController_Category_Item") as! UINavigationController
        let editController = navigation.topViewController as! CategoryDetailTableViewController
        editController.categoryDetailTableDelegate = self
        editController.item = item
        presentViewController(navigation, animated: true, completion: nil)
    }
    override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
        let catgegoryItem = categories[indexPath.row]
        performSegueWithIdentifier("showTasks", sender: catgegoryItem)
    }
  

    
    // MARK: - Navigation

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showTasks" {
            let targetVC = segue.destinationViewController as! TableViewController
            let catgegoryItem = sender as! CategoryClass
            //print(catgegoryItem.name)
            targetVC.catgegoryItem = catgegoryItem
        } else if segue.identifier == "category_DetaiSegue" {
            let targetNV = segue.destinationViewController as! UINavigationController
            let targetVC = targetNV.topViewController as! CategoryDetailTableViewController
            targetVC.categoryDetailTableDelegate = self
            
        }
    }

}

extension CategoryTableViewController: CategoryDetailTableDelegate {
    func addItemFunc(UITableViewController: CategoryDetailTableViewController, addItem item: CategoryClass) {
        categories.append(item)
        let indexPath = NSIndexPath(forRow: categories.count-1, inSection: 0)
        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Bottom)
        
    }
    
    func editItemFunc(UITableViewController: CategoryDetailTableViewController, editFinished item: CategoryClass) {
        print("编辑")
        if let indexPath = categories.indexOf(item){
            categories[indexPath] = item
            let indexPathofcell = NSIndexPath(forRow: indexPath, inSection: 0)
            tableView.reloadRowsAtIndexPaths([indexPathofcell], withRowAnimation: .Left)
            
        }
        
        
        
        
    }
}
